from django.db import models
from category.models import Category
# Create your models here.


class HotRecommend(models.Model):
    name = models.CharField(max_length=100,verbose_name="推荐名称")
    description = models.TextField(verbose_name="推荐描述")
    created_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间",null=True,blank=True)
    pid=models.ForeignKey('self',on_delete=models.CASCADE,verbose_name="所属父级推荐",null=True,blank=True)
    type=models.IntegerField(verbose_name="推荐类型",null=True,blank=True)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = "热门推荐"
        verbose_name_plural = verbose_name


class Product(models.Model):
    name = models.CharField(max_length=100,verbose_name="商品名称")
    description = models.TextField(verbose_name="商品描述")
    original_price = models.DecimalField(max_digits=10, decimal_places=2,verbose_name="原价")
    stock = models.IntegerField(verbose_name="商品库存")
    category = models.ForeignKey(Category, on_delete=models.CASCADE,verbose_name="商品分类")
    cover = models.ImageField(upload_to='media/goodsCover/',verbose_name="商品封面",null=True,blank=True)
    sold_quantity = models.IntegerField(default=0, verbose_name="售出数量")
    # 新增字段
    discount_or_not=models.BooleanField(default=False,verbose_name="是否打折")
    current_price=models.DecimalField(max_digits=10,decimal_places=2,verbose_name="现价",default=0)
    hot_recommend=models.ForeignKey(HotRecommend,on_delete=models.CASCADE,verbose_name="推荐",null=True,blank=True)
    # 后增加字段，方便前端判断是否有属性，从而决定是否打开sku插件
    has_attr=models.BooleanField(default=True,verbose_name="是否有属性")
    def __str__(self):
        return self.name
    class Meta:
        # 这意味着如果违反了这个条件，数据库会拒绝插入或更新数据。
        models.CheckConstraint(
            check=models.Q(stock__gte=0),
            name='stock_not_negative',
        ),
        models.CheckConstraint(
            check=models.Q(originalPrice__gte=0),
            name='original_price_not_negative',
        ),
        models.CheckConstraint(
            check=models.Q(current_price__gte=0),
            name='current_price_not_negative',
        ),
        verbose_name = "商品"
        verbose_name_plural = verbose_name


class HistoryProduct(models.Model):
    product=models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name="商品",null=True,blank=True)
    user=models.ForeignKey("user.UserProfile",on_delete=models.CASCADE,verbose_name="用户",null=True,blank=True)
    created_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间",null=True,blank=True)
    constraints = [
        # 新增约束，限制数据库中最多存储3条数据,一个用户的数据量不能过多
        models.UniqueConstraint(fields=['user'], name='limit_product_count', condition=models.Q(user__lte=3)),
        # 联合唯一约束，防止重复无效数据
        models.UniqueConstraint(fields=['user', 'product'], name='unique_like_product'),
    ]
    def __str__(self):
        return self.product.name
    class Meta:
        verbose_name = "历史商品"
        verbose_name_plural = verbose_name


class ProductBanners(models.Model):
    product=models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name="商品",null=True,blank=True)
    image=models.ImageField(upload_to='media/goods_banner/',verbose_name="商品轮播图",null=True,blank=True)



